// pages/vip_index/vip_index.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');



Page({

  /**
   * 页面的初始数据
   */
  data: {
    userList: {}, //用户数据
    levels: {}, //会员数据
    goodsList: {}, //兑换列表
    Getshow:false,//签到弹出
    seeshow:false,//获取积分弹出
    data:{userPoints: "391", continuousDay: "1", point: "50"}
  },

  // 跳转积分任务
  onIntegral() {
    wx.navigateTo({
      url: '../integral/integral',
    })
  },


  // 跳转积分明细
  onDetailed() {
    wx.navigateTo({
      url: '../integral_detailed/integral_detailed',
    })
  },


  // 跳转兑换列表
  onIntegral_list() {
    wx.navigateTo({
      url: '../integral_list/integral_list?typeId='+0,
    })
  },

  // 跳转会员规则
  onRule() {
    wx.navigateTo({
      url: '../rule/rule',
    })
  },



  // 会员中心数据
  getVipdata: function() {
    var that = this;
    util.request(api.MemberCenter, {}, 'get').then(function(res) {
      if (res.errno === 0) {
        that.setData({
          userList: res.data.user,
        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }

    });
  },

  // 签到查询
  getInregral() {
    var that = this;
    util.request(api.Integral, {}, 'get').then(function(res) {
      if (res.errno === 0) {
        console.log(res.data.assignmentInfos[0])
        that.setData({
          assignmentInfosList: res.data.assignmentInfos,
          first: res.data.assignmentInfos[0].valus.split("_")[0],
          last: res.data.assignmentInfos[0].valus.split("_")[1],
        })


      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }


    });

  },

  // 兑换列表查询
  getData() {
    var that = this;
    util.request(api.ExchangeList, {
      exchangeType: '',
    }, 'get').then(function(res) {
      if (res.errno === 0) {
        console.log(res)
        that.setData({
          intrgralList: res.data.list.slice(0, 2),
        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }
    });
  },

  //兑换商品
  onDatail(e) {
    console.log(e)
    let goodsId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../integral_sec/integral_sec?goodsId=' + goodsId,
    })
  },
  // 兑换优惠券
  onCoupon(e) {
    console.log(e)
    let cardsId = e.currentTarget.dataset.id
    var that = this;
    util.request(api.PointExchange, {
      id: cardsId,
    }, 'post').then(function(res) {
      if (res.errno === 0) {
        console.log(res)
        wx.showToast({
          title: "兑换成功",
          icon: 'none',
          duration: 1500
        })
        setTimeout(function() {
          that.getData()
        }, 1000)
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }
    });
  },



  // 签到弹出框
    toPopup() {
    this.setData({
      Getshow: true
    });
  },

  ditClose() {
    
    this.setData({
      Getshow: false
    });
    this.getInregral()
  },


  // 积分获得提示
  upget(){
    this.setData({
      seeshow: true
    });
  },
   
  noget(){
    this.setData({
      seeshow: false
    });
  },

  // 签到
  getTo(e) {
    let type = e.currentTarget.dataset.id
    // 获取签到时间
    let myDate = util.formatTime(new Date());
    console.log(myDate)
    var that = this;
    util.request(api.ReceiveUp, {
      type: type,
      time: myDate
    }, 'post').then(function(res) {
      if (res.errno === 0) {
        //  var data = {userPoints: "391", continuousDay: "1", point: "50"}
        that.setData({
          Datelist:res.data
        })
        if(res.data.continuousDay == 1){
            that.upget()  
        }

        
        
        that.toPopup()

      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }


    });


  },







  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取会员中心数据
    this.getVipdata()
    // 签到查询
    this.getInregral()
    // 兑换列表查询
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})